package eu.jba.denshijisho.adapter;

import java.util.List;

import eu.jba.denshijisho.R;
import eu.jba.denshijisho.entity.Configuration;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ConfigurationAdapter extends ArrayAdapter<Configuration>{
	
	private Context context;
	private int layoutResourceId;
	private List<Configuration> data;
	
	public ConfigurationAdapter(Context context,
			int layoutResourceId, List<Configuration> data) {
		super(context, layoutResourceId, data);
		this.context = context;
		this.layoutResourceId = layoutResourceId;
		this.data = data;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		View row = convertView;
		ConfigurationHolder holder;
		Configuration configuration = data.get(position);
		
		if( row == null ){
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
			holder = new ConfigurationHolder();
			row = inflater.inflate(layoutResourceId, parent, false);
			holder.configurationName = (TextView)row.findViewById(R.id.configurationName_text_view_item_row1);
			holder.integerValue = (TextView)row.findViewById(R.id.integerValue_text_view_item_row1);
			holder.stringValue = (TextView)row.findViewById(R.id.stringValue_text_view_item_row1);
			row.setTag( holder );
		}else{
			holder = (ConfigurationHolder)row.getTag();
		}
		
		holder.configurationName.setText( configuration.getVariableName() );
		holder.integerValue.setText( configuration.getIntegerValue().toString() );
		holder.stringValue.setText( configuration.getStringValue() );
		return row;
	}

	private static class ConfigurationHolder{
		private TextView configurationName;
		private TextView integerValue;
		private TextView stringValue;
	}
	
}
